<template>
  <div class='container'>
    <el-card class="box-card">
      <!-- 顶部搜索栏 -->
        <el-form ref="form"  :inline="true" :model="form" label-width="80px" label-position='right'>
        <el-row >
          <el-col :span="18">
              <el-form-item label="关键字">
                <el-input v-model.trim="form.keyword"  placeholder="根据文章标题搜索"></el-input>
              </el-form-item>
          </el-col>
          <el-col :span="6" class="success-button">
              <el-form-item>
                <el-button  @click="resetForm('numberValidateForm')">清除</el-button>
                <el-button type="primary" @click="loadList">搜索</el-button>
              </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 提示文案 -->
      <el-alert
        type="info"
        show-icon>
        <template slot="title">
          数据一共{{total}} 条
        </template>
      </el-alert>
      <!-- 表格 -->
      <el-table
        :data="tableData"
        v-loading="loading"
        style="width: 100; margin: 10px 0">
        <el-table-column
          label="编号"
          prop="id"
          width="220">
        </el-table-column>
        <el-table-column
          prop="title"
          label="题型"
          width="80">
          <template slot-scope="{row}">
            {{row.questionType}}
          </template>
        </el-table-column>
        <el-table-column
          prop="visits"
          width="220"
          label="题目编号">
           <template slot-scope="{row}">
            <div style="color:blue" v-for="(item,index) in row.questionIDs" @click="onPreview(item.id)" :key="index">{{item.number}}</div>
          </template>
        </el-table-column>
        <el-table-column
          width="180"
          label="录入时间">
          <template slot-scope="{row}">
            {{row.addTime | date}}
          </template>
        </el-table-column>
        <el-table-column
          prop="totalSeconds"
          label="答题时间（s）">
        </el-table-column>
        <el-table-column
          prop="accuracyRate"
          label="正确率（%）">
        </el-table-column>
        <el-table-column
          prop="userName"
          label="录入人">
        </el-table-column>
        <el-table-column
          prop="address"
          width="80"
          label="操作">
          <template slot-scope="{row}">
            <el-button type="danger"   icon="el-icon-delete" plain circle @click="onDelete(row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <page-nation :total='total' :formlist='formList' @on-success='onChangePage'/>
      <!-- 题目预览 -->
      <QuestionsPreview v-if="view" v-model='view' :id='id' @yt='view=false'/>
    </el-card>
  </div>
</template>

<script>
import PageNation from '../components/PageNation.vue'
import { randoms, removeRandoms } from '@/api/hmmm/questions.js'
import QuestionsPreview from '../components/questions-preview.vue'
import dayjs from 'dayjs'
export default {
  components: { PageNation, QuestionsPreview },
  data () {
    return {
      form: {
        keyword: ''
      },
      loading: true,
      tableData: [],
      formList: {
        page: 1,
        pagesize: 10 // 页尺寸
      },
      total: 1, // 共几页,
      view: false, // 查看
      id: 0
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    // 预览
    onPreview (id) {
      console.log(111)
      this.view = true
      this.id = id
      console.log(this.view)
    },
    // 加载数据
    async loadList () {
      this.loading = true
      const obj = { ...this.formList }
      if (this.form.keyword !== '') {
        obj.keyword = this.form.keyword
      }
      const res = await randoms(obj)
      console.log(res)
      this.loading = false
      this.tableData = res.data.items
      this.total = res.data.counts
      this.row = {}
    },
    // 更改页数
    onChangePage (val) {
      this.formList.page = val.pagenum
      this.formList.pagesize = val.pagesize
      this.loadList()
    },
    // 重置
    resetForm () {
      console.log(1111)
      this.$refs.form.resetFields()
      this.form = {
        keyword: '',
        type: ''
      }
    },
    // 删除
    async onDelete (row) {
      const res = await removeRandoms({
        id: row.id
      })
      this.$message.success('删除成功')
      console.log(res, '删除成功')
      this.loadList()
    }
  },
  filters: {
    date (val) {
      return dayjs(dayjs(val).$d).format('YYYY-MM-DD HH:mm:ss')
    }
  }
}
</script>

<style scoped lang='less'>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  // width: 100%;
  margin: 10px;
  // box-sizing: border-box;
}
.success-button {
  text-align: right;
}
</style>
